package Logg_Inn;
import model.*;

import gui_kalenderpanel.VisningsPanel;

import javax.swing.*;  

import java.awt.*;  
import java.awt.event.*;  
  
public class LoggInnPanel extends JPanel{  
  
    JTextField usernameTxt;  
    JTextField passwordTxt;  
    JFrame loginFrame;
    KalenderKontroll kalkont;
    
    public LoggInnPanel(KalenderKontroll temp) {
    	
    	loginFrame = new JFrame("Logg inn");
    	kalkont = temp;
    	setLayout(new GridBagLayout());
    	GridBagConstraints c = new GridBagConstraints();
    	setSize(350,200);
    	usernameTxt = new JTextField(25);
    	passwordTxt = new JTextField(25);
    	JLabel userLab = new JLabel("Brukernavn: ");
    	JLabel passLab = new JLabel("Passord: ");
    	JButton loginButton = new JButton("Logg inn");
    	loginButton.addActionListener(new loginButtonListener());
    	
    	c.gridx = 0;
    	c.gridy = 0;
    	
    	add(userLab, c);
    	c.gridx = 1;
    	add(usernameTxt, c);
    	c.gridx = 0;
    	c.gridy = 1;
    	add(passLab, c);
    	c.gridx = 1;
    	add(passwordTxt, c);
    	c.gridy = 2;
    	add(loginButton, c);
    	
    	loginFrame.setLayout(new BorderLayout());
    	loginFrame.add(this, BorderLayout.CENTER);
    	loginFrame.setSize(500, 300);
    	loginFrame.setLocationRelativeTo(null);
    	loginFrame.setVisible(true);
    	loginFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
      

//        loginFrame.getContentPane().add(BorderLayout.CENTER,loginPanel);  
////        mainFrame.getContentPane().add(BorderLayout.CENTER,mainPanel);  
//  
////        loginFrame.setVisible(true);                      
////        mainFrame.setVisible(true);       

  
  
  
    public class loginButtonListener implements ActionListener{  
        public void actionPerformed(ActionEvent ev){  
        	System.out.println(usernameTxt.getText() + " " + passwordTxt.getText());
        	Bruker temp = kalkont.hentBruker(usernameTxt.getText(), passwordTxt.getText());
        	if (temp != null) {
        		
        		kalkont.setBruker(temp); 
        		loginFrame.setVisible(false); 
        		VisningsPanel pane = new VisningsPanel(kalkont, kalkont.getBruker());
        		JFrame testFrame = new JFrame("Avtale/Beskjed");
        		testFrame.add(pane);
        		testFrame.setSize(600,600);
        		testFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        		testFrame.setLocationRelativeTo(null);
        		testFrame.pack();
        		testFrame.setVisible(true);
        	}
        }  
    }  
} 
